package com.ruoyi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RequestWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.SysProd;
import com.ruoyi.order.service.ISysProdService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/prod")
public class SysProdController extends BaseController
{
    @Autowired
    private ISysProdService sysProdService;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("order:prod:list")
    @GetMapping("/list")
    public TableDataInfo list(SysProd sysProd)
    {
        startPage();
        List<SysProd> list = sysProdService.selectSysProdList(sysProd);
        return getDataTable(list);
    }

    /**
     * 查询商品列表(从ES获取)
     */
    @RequiresPermissions("order:prod:list")
    @GetMapping("/listFromES")
    public TableDataInfo listFromES(HttpServletRequest request,SysProd sysProd)
    {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        TableDataInfo data = sysProdService.getListFromEs(sysProd,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        return data;
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("order:prod:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProd sysProd)
    {
        List<SysProd> list = sysProdService.selectSysProdList(sysProd);
        ExcelUtil<SysProd> util = new ExcelUtil<SysProd>(SysProd.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("order:prod:query")
    @GetMapping(value = "/{prodId}")
    public AjaxResult getInfo(@PathVariable("prodId") Long prodId)
    {
        return success(sysProdService.selectSysProdByProdId(prodId));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("order:prod:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProd sysProd)
    {
        return toAjax(sysProdService.insertSysProd(sysProd));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("order:prod:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProd sysProd)
    {
        return toAjax(sysProdService.updateSysProd(sysProd));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("order:prod:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prodIds}")
    public AjaxResult remove(@PathVariable Long[] prodIds)
    {
        return toAjax(sysProdService.deleteSysProdByProdIds(prodIds));
    }

    /**
     * 创建商品ES索引
     */
    @PostMapping("/createProdIndex")
    public AjaxResult createProdIndex(){
        return sysProdService.createProdIndex();
    }

    /**
     * 将商品数据同步到ES
     */
    @PostMapping("/syncProdIndex")
    public AjaxResult syncProdIndex(){
        return sysProdService.syncProdIndex();
    }


}
